package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.FootXf;
import org.footballmanager.pandafootball.system.form.impl.FootXfSearchForm;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Controller
@SessionAttributes("footXfSearchForm")
@RequestMapping("/hy/footXf")
public class HyXfController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footXf";
	private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

	@Autowired
	private FootXfService footXfService;
	
	@Autowired
	private FootPersonService footPersonService;
	
	@Autowired
	private FootTeamService footTeamService;
	
	@Autowired
	private FootTaxService footTaxService;
	
	@Autowired
	private FootUserPersonService footUserPersonService;
	
	@Autowired
	private FootUserService footUserService;
	
	@Autowired
	private FootZjService footZjService;

    /**
     * 初始化
     * @param map 参数
     * @param footXfSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm){
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        FootXfSearchForm newFootXfSearchForm = new FootXfSearchForm();
        if(footXfSearchForm != null){
            newFootXfSearchForm = footXfSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootXfSearchForm);
    }

	@RequestMapping(value="/search")
	public String search(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
        footXfSearchForm.setUserId(user.getId());
        footXfService.search(ENTITYNAME, map, footXfSearchForm);
		return LISTPAGE;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String openAddPage(Model map, @RequestParam Long id, int xycs) {
		FootXf xf = new FootXf();
		xf.setXycs(xycs);
		FootPerson fp = footPersonService.get(id);
		if(null != fp){
			xf.setQyId(id);
			xf.setQyName(fp.getName());
			xf.setYw(fp.getYw());
		}
		xf.setXfsj(new Date());
		map.addAttribute(ENTITYNAME, xf);
		return ADDPAGE;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String save(@Valid @ModelAttribute FootXf footXf, BindingResult bindingResult,
			HttpServletRequest request, SessionStatus s) {
		if (!bindingResult.hasErrors()) {
			FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
            footXf.setUserId(user.getId());
            footXf.setUserName(user.getNike());
			FootPerson fp = footPersonService.get(footXf.getQyId());
			FootTeam team = footTeamService.getTeamByUserid(user.getId());
			if(fp != null){
                footXf.setQyName(fp.getName());
                footXf.setYw(fp.getYw());
				//返还球员身价的30%
				double result = Double.valueOf(fp.getQysj()) * footTaxService.getDismissTax();
				team.setQdZzj(team.getQdZzj() + result);
				footTeamService.updateNotNull(team, request);
				footZjService.sendZj(team.getUserId(), result, true, "因解约球员" + fp.getName() + ",增加€" +
						new DecimalFormat(",###").format(result) + ",当前的球队总资金为:€" +
						new DecimalFormat(",###").format(team.getQdZzj()), request);
			}
			footXfService .save(footXf, request);
			s.setComplete();
			return "redirect:/hy/footClub/viewList";
		} else 
			return ADDPAGE;
	}

	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		List<FootXf> list = footXfService.findByUserId(user.getId());
        map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		return VIEWPAGE;
	}

    /**
     * 续约
     * @param request 请求
     * @param qdId 球员主键
     * @param xycs 续约次数
     * @return 成功或失败
     */
	@RequestMapping(value = "/xy", method = RequestMethod.POST)
	@ResponseBody
	public String xuyue(HttpServletRequest request, Long qdId, Integer xycs) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		return footUserPersonService.renew(user.getId(), qdId, xycs, request);
	}
}
